package com.etoak.jwt;


import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtil {

    public static final String SECRET = "11111111111111111111111111111111";

    /* 签名用的Key */
    private static Key KEY = Keys.hmacShaKeyFor(SECRET.getBytes());

    /* 12小时有效期 */
    private static final long EXPIRE = 1000 * 60 * 60 * 12;

    /* 创建Jwt */
    public static String create(Map<String, Object> claimsMap) {
        Date now = new Date();
        Date expiration = new Date(now.getTime() + EXPIRE);
        return Jwts.builder()
                .signWith(KEY)// 签名密钥
                .setClaims(claimsMap)// 自定义payload
                .setIssuedAt(now) // 签发时间
                .setExpiration(expiration) // 过期时间

                .compact();
    }

    public static Map<String, Object> parse(String jwt) {
        return Jwts.parserBuilder()
                .setSigningKey(KEY)
                .build()
                .parseClaimsJws(jwt)
                .getBody();
    }
}
